package com.example.a2032.fragment;

import android.content.Intent;
import android.util.Log;

import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;

import com.example.a2032.Utils.ResolveUtil;
import com.example.a2032.Utils.RxBus;
import com.example.a2032.Utils.SerialPortUtil;
import com.example.a2032.Utils.Utils;
import com.example.a2032.db.DataDbHelper;
import com.example.a2032.listener.DeviceDataListener;
import com.example.a2032.entity.ActionData;
import com.example.a2032.entity.BleData;
import com.example.a2032.entity.EcgMeasureResult;
import com.example.a2032.entity.PPGMeasureData;
import com.example.a2032.entity.RealTimeData;
import com.example.a2032.entity.RealTimeStepData;
import com.example.a2032.entity.SleepData;
import com.example.a2032.entity.WatchUserInfo;

import java.util.Calendar;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BaseFragment extends Fragment implements DeviceDataListener{
    protected void unSubscribe(Disposable disposable){
        if(disposable!=null&&!disposable.isDisposed()){
            disposable.dispose();;
        }
    }
    protected void startActivity(Class activity){
        Intent intent=new Intent(getContext(),activity);
        startActivity(intent);
    }
    protected void startActivity(Class activity,String key,String value){
        Intent intent=new Intent(getContext(),activity);
        intent.putExtra(key,value);
        startActivity(intent);
    }
    //替换系统动画
    protected void startActivityWithTrans(Class activity,String key,String value){
        if(activity==null)return;
        ActivityOptionsCompat activityOptionsCompat=ActivityOptionsCompat.makeCustomAnimation(getActivity(),android.R.anim.fade_in,android.R.anim.fade_out);
        Intent intent=new Intent(getContext(),activity);
        intent.putExtra(key,value);
        ActivityCompat.startActivity(getContext(),intent,activityOptionsCompat.toBundle());
      //  startActivity(intent);
    }

    private static final String TAG = "BaseFragment";
    Disposable disposable;
    protected void subscribe(DeviceDataListener deviceDataListener) {

        RxBus.getInstance().toObservable(BleData.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BleData>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(BleData bleData) {
                String action = bleData.getAction();
                if (action.equals(ActionData.BleData)) {
                    byte[] value = bleData.getValue();
                    ResolveUtil.postData(value, deviceDataListener);
                }

            }


            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
        unSubscribe(disposable);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        Log.i(TAG, "onDestroyView: "+this.getClass().getSimpleName());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach: ");
    }

    @Override
    public void DeviceAddress(String address) {

    }

    @Override
    public void DeviceVersion(String version) {

    }

    @Override
    public void DeviceTime(Calendar calendar) {

    }

    @Override
    public void DeviceUserInfo(WatchUserInfo watchUserInfo) {

    }

    @Override
    public void RealTimeData(RealTimeStepData timeStepData) {
        RealTimeData realTimeData=new RealTimeData();
        String date= Utils.getTodayTimeString();
        realTimeData.setDate(date);
        realTimeData.setDistance(timeStepData.getDistance());
        realTimeData.setSteps(timeStepData.getStep());
        realTimeData.setCal(timeStepData.getCal());
        Log.i(TAG, "RealTimeData: save ");
        DataDbHelper.getInstance(getContext()).getRealTimeStepDao().insert(realTimeData);
    }

    @Override
    public void PPGMeasureData(PPGMeasureData ppgMeasureData) {

    }

    @Override
    public void EcgMeasureData(EcgMeasureResult ecgMeasureResult) {

    }

    @Override
    public void SleepData(SleepData sleepData,boolean finish) {

    }

    @Override
    public void TempData(float tempData) {

    }

    protected void sendData(byte[] value) {
        SerialPortUtil.getInstance().sendSerialPort(value);
    }
}
